package me.heyclock.hl.recordwriter.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.support.constraint.ConstraintLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.HashMap;

import me.heyclock.hl.recordwriter.tools.AppUtils;

/**
 * 悬浮View管理
 */
public class WinManager {

    /**
     * 存储多个悬浮控件
     */
    private static HashMap<String, Object[]> windowManagerHashMap = new HashMap<>();

    /**
     * 添加悬浮按钮
     * @param context
     * @param layoutId
     */
    public static void add(Activity context, int layoutId,
                           int width, int height,
                           int x, int y,
                           final View.OnClickListener onClickListener){
        if (windowManagerHashMap.containsKey(context.getLocalClassName())){
            return;
        }
        WindowManager windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        View view = LayoutInflater.from(context.getApplication()).inflate(layoutId, null);
        {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); ++i){
                viewGroup.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickListener.onClick(v);
                    }
                });
            }
        }
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        //params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        ///< 模拟器只能用这种类型
        if (AppUtils.getModel().contains("Android SDK built for")){
            params.type = WindowManager.LayoutParams.TYPE_TOAST;
        }else {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.format= PixelFormat.TRANSLUCENT;
        params.width = width;
        params.height = height ;

        ///< 设置一个基准，这样x、y才会生效【不设置或者center都会导致无效】
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = x;
        params.y = y;
        params.alpha = 1.0f;
        windowManager.addView(view, params);
        windowManagerHashMap.put(context.getLocalClassName(), new Object[]{windowManager, view});
        //windowManager.updateViewLayout(view, mWmParams);
    }

    /**
     * 删除悬浮窗
     * @param context
     */
    public static void removeView(Activity context){
        if (windowManagerHashMap.containsKey(context.getLocalClassName())){
            Object[] objects = windowManagerHashMap.get(context.getLocalClassName());
            ((WindowManager)objects[0]).removeView((View) objects[1]);
            windowManagerHashMap.remove(context.getLocalClassName());
        }
    }

    /**
     * 隐藏录音按钮
     * @param context
     */
    public static void hideView(Activity context){
        if (windowManagerHashMap.containsKey(context.getLocalClassName())){
            Object[] objects = windowManagerHashMap.get(context.getLocalClassName());
            ((View) objects[1]).setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 显示录音按钮
     * @param context
     */
    public static void showView(Activity context){
        if (windowManagerHashMap.containsKey(context.getLocalClassName())){
            Object[] objects = windowManagerHashMap.get(context.getLocalClassName());
            ((View) objects[1]).setVisibility(View.VISIBLE);
        }
    }
}
